<?php

namespace App\Admin\Metrics\Charts;

use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\ApexCharts\Chart;
use Carbon\Carbon;
use App\Models\Order;

class OrderLIne extends Chart
{
    public function __construct($containerSelector = null, $options = [])
    {
        parent::__construct($containerSelector, $options);

        $this->setUpOptions();
    }

    /**
     * 初始化图表配置
     */
    protected function setUpOptions()
    {
        $color = Admin::color();

        $colors = ['#008FFB', '#00E396'];
        $title = '近 7 日走势图';
        $this->options([
            'colors' => $colors,
            'chart' => [
                'type' => 'area',
                'height' => 430,
                'dropShadow'=>[
                    'enabled'=> true,
                    'color'=>'#000',
                    'top'=> 18,
                    'left'=> 7,
                    'blur'=> 10,
                    'opacity'=> 0.2
                ],
                'toolbar'=>[
                    'show'=>false
                ]
            ],
            'dataLabels'=>[
                'enabled'=>true,
            ],
            'stroke'=>[
                'curve'=>'smooth'
            ],
            'title'=>[
                'text'=>$title,
                'align'=> 'left'
            ],
            'xaxis' => [
                'categories' => [],
            ],
        ]);
    }

    /**
     * 处理图表数据
     */
    protected function buildData()
    {
        $today = Carbon::today();
        $categories = [substr($today,0,10),substr($today->addDays(-1),0,10),substr($today->addDays(-1),0,10),substr($today->addDays(-1),0,10),substr($today->addDays(-1),0,10),substr($today->addDays(-1),0,10),substr($today->addDays(-1),0,10)];
        sort($categories);

        //订单数
        $order_count_01 = Order::paid()->whereDate('paid_at', '>=', Carbon::today())->count();
        $order_count_02 = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-1))->whereDate('paid_at', '<', Carbon::today())->count();
        $order_count_03 = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-2))->whereDate('paid_at', '<', Carbon::today()->addDays(-1))->count();
        $order_count_04 = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-3))->whereDate('paid_at', '<', Carbon::today()->addDays(-2))->count();
        $order_count_05 = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-4))->whereDate('paid_at', '<', Carbon::today()->addDays(-3))->count();
        $order_count_06 = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-5))->whereDate('paid_at', '<', Carbon::today()->addDays(-4))->count();
        $order_count_07 = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-6))->whereDate('paid_at', '<', Carbon::today()->addDays(-5))->count();
        
        //订单金额
        $order_price_01 = Order::paid()->whereDate('paid_at', '>=', Carbon::today())->sum('price');
        $order_price_02 = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-1))->whereDate('paid_at', '<', Carbon::today())->sum('price');
        $order_price_03 = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-2))->whereDate('paid_at', '<', Carbon::today()->addDays(-1))->sum('price');
        $order_price_04 = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-3))->whereDate('paid_at', '<', Carbon::today()->addDays(-2))->sum('price');
        $order_price_05 = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-4))->whereDate('paid_at', '<', Carbon::today()->addDays(-3))->sum('price');
        $order_price_06 = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-5))->whereDate('paid_at', '<', Carbon::today()->addDays(-4))->sum('price');
        $order_price_07 = Order::paid()->whereDate('paid_at', '>=', Carbon::today()->addDays(-6))->whereDate('paid_at', '<', Carbon::today()->addDays(-5))->sum('price');

        // 执行你的数据查询逻辑
        $data = [
            [
                'name'=> "订单数",
                'data' => [$order_count_07,$order_count_06, $order_count_05, $order_count_04, $order_count_03, $order_count_02, $order_count_01]
            ],
            [
                'name'=> "订单金额/元",
                'data' => [$order_price_07,$order_price_06,$order_price_05,$order_price_04,$order_price_03,$order_price_02,$order_price_01]
            ]
        ];
        $this->withData($data);
        $this->withCategories($categories);
    }

    /**
     * 设置图表数据
     *
     * @param array $data
     *
     * @return $this
     */
    public function withData(array $data)
    {
        return $this->option('series', $data);
    }

    /**
     * 设置图表类别.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withCategories(array $data)
    {
        return $this->option('xaxis.categories', $data);
    }

    /**
     * 渲染图表
     *
     * @return string
     */
    public function render()
    {
        $this->buildData();

        return parent::render();
    }
}